package com.js.nickcar.module.addcar.brand;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.js.nickcar.R;
import com.js.nickcar.base.BaseHolder;
import com.js.nickcar.entity.remote.CarBrandBean;

import java.util.ArrayList;

/**
 * Created by LZY on 2017/9/29.
 */
public class CarBrandAdapter extends RecyclerView.Adapter implements CarBrandBean.TYPE, HotCarBrandAdapter.OnHotItemClickListner {
    public static final int VIEW_TYPE_HOT = 0;//热门条目
    public static final int VIEW_TYPE_LETTER = 1;//字母条目
    public static final int VIEW_TYPE_NORMAL = 2;//普通条目

    public final Context mContext;
    public final LayoutInflater mInflater;
    public ArrayList<CarBrandBean> mHotCarBrandList = new ArrayList<>();
    public ArrayList<CarBrandBean> mItems = new ArrayList<>();

    public CarBrandAdapter(Context context) {
        super();
        mContext = context;
        mInflater = LayoutInflater.from(mContext.getApplicationContext());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_HOT:
                HotHolder hotHolder = new HotHolder(mInflater.inflate(R.layout.layout_hot_car_brand, parent, false));
                RecyclerView rvHotBrand = hotHolder.rvHotBrand;
                rvHotBrand.setLayoutManager(new GridLayoutManager(mContext, 5));
                HotCarBrandAdapter hotCarBrandAdapter = new HotCarBrandAdapter(mContext);
                hotCarBrandAdapter.setOnHotItemClickListner(this);
                rvHotBrand.setAdapter(hotCarBrandAdapter);
                return hotHolder;
            case VIEW_TYPE_LETTER:
                return new LetterHoder(mInflater.inflate(R.layout.item_car_brand_letter, parent, false));
            default:
                return new CarBrandHoder(mInflater.inflate(R.layout.item_car_brand, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_HOT:
                onBindHotHolder((HotHolder) holder);
                break;
            case VIEW_TYPE_LETTER:
                onBindLetterHolder((LetterHoder) holder, getItem(position - 1), position);
                break;
            default:
                onBindCarBrandHolder((CarBrandHoder) holder, getItem(position - 1), position);
                break;
        }
    }

    private void onBindHotHolder(HotHolder holder) {
        HotCarBrandAdapter adapter = (HotCarBrandAdapter) holder.rvHotBrand.getAdapter();
        if (adapter == null) {
            adapter = new HotCarBrandAdapter(mContext);
            holder.rvHotBrand.setAdapter(adapter);
        }
        adapter.setData(mHotCarBrandList);
    }

    private void onBindLetterHolder(LetterHoder holder, CarBrandBean item, int position) {
        holder.tvLetter.setText(item.getPin().toUpperCase());
    }

    private void onBindCarBrandHolder(CarBrandHoder holder, final CarBrandBean item, final int position) {
        holder.tvBrandName.setText(item.getBrand_name());
        holder.tvBrandName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(item, position, VIEW_TYPE_NORMAL);
                }
            }
        });
    }

    @Override
    public void onHotItemClick(CarBrandBean carBrandBean, int position) {
        onItemClickListener.onItemClick(carBrandBean, position, VIEW_TYPE_HOT);
    }

    private CarBrandBean getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return VIEW_TYPE_HOT;
        return TYPE_LETTER.equals(getItem(position - 1).getType()) ? VIEW_TYPE_LETTER : VIEW_TYPE_NORMAL;
    }

    @Override
    public int getItemCount() {
        return mItems.size() + 1;
    }

    public void updateHot(ArrayList<CarBrandBean> hotLists) {
        if (hotLists == null) return;
        mHotCarBrandList.clear();
        mHotCarBrandList.addAll(hotLists);
        notifyItemChanged(0);
    }

    public void addAll(ArrayList<CarBrandBean> data) {
        if (data == null) return;
        mItems.clear();
        mItems.addAll(data);
        notifyDataSetChanged();
    }


    private static class HotHolder extends BaseHolder {
        RecyclerView rvHotBrand;

        public HotHolder(View view) {
            super(view);
            rvHotBrand = findViewById(R.id.rv_hot_brand);
        }
    }

    private static class LetterHoder extends BaseHolder {
        public TextView tvLetter;

        public LetterHoder(View view) {
            super(view);
            tvLetter = findViewById(R.id.tv_letter);
        }
    }

    private class CarBrandHoder extends BaseHolder {
        public TextView tvBrandName;

        public CarBrandHoder(View view) {
            super(view);
            tvBrandName = findViewById(R.id.tv_brand_name);
        }
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(CarBrandBean item, int position, int itemType);
    }
}
